import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:fluwx/fluwx.dart' as wx;

export 'package:fluwx/fluwx.dart';

class WxSdkUtils {
  static WxSdkUtils? _instance;

  static WxSdkUtils getInstance() {
    if (_instance == null) {
      _instance = new WxSdkUtils._();
    }
    return _instance!;
  }

  WxSdkUtils._();
  // 唤起登录回调
  Function(wx.WeChatAuthResponse res)? _sendWeChatAuthCallback;

  // 唤起分享回调
  Function(wx.WeChatShareResponse res)? _shareToWeChatCallback;

  // 唤起支付回调
  Function(wx.WeChatPaymentResponse res)? _payWithWeChatCallback;
  // 唤起消息订阅回调
  Function(wx.WeChatSubscribeMsgResponse res)? _subscribeWeChatMsgCallback;

  // 初始化
  void init(String appId, String universalLink) {
    wx.weChatResponseEventHandler.listen((res) {
      if (res is wx.WeChatAuthResponse) {
        if (_sendWeChatAuthCallback != null) {
          _sendWeChatAuthCallback!(res);
          _sendWeChatAuthCallback = null;
        }
      } else if (res is wx.WeChatShareResponse) {
        if (_shareToWeChatCallback != null) {
          _shareToWeChatCallback!(res);
          _shareToWeChatCallback = null;
        }
      } else if (res is wx.WeChatPaymentResponse) {
        if (_payWithWeChatCallback != null) {
          _payWithWeChatCallback!(res);
          _payWithWeChatCallback = null;
        }
      } else if (res is wx.WeChatSubscribeMsgResponse) {
        if (_subscribeWeChatMsgCallback != null) {
          _subscribeWeChatMsgCallback!(res);
          _subscribeWeChatMsgCallback = null;
        }
      }
    });
    wx.registerWxApi(appId: appId, universalLink: universalLink);
  }

  // 调起微信登录
  Future<wx.WeChatAuthResponse> sendWeChatAuth({
    String scope = 'snsapi_userinfo',
    String state = 'state',
  }) {
    Completer<wx.WeChatAuthResponse> completer =
        new Completer<wx.WeChatAuthResponse>();
    _sendWeChatAuthCallback = (res) {
      completer.complete(res);
    };
    wx.sendWeChatAuth(scope: scope, state: state);
    return completer.future;
  }

  // 分享
  Future<wx.WeChatShareResponse> shareToWeChat(wx.WeChatShareBaseModel model) {
    Completer<wx.WeChatShareResponse> completer =
        new Completer<wx.WeChatShareResponse>();
    _shareToWeChatCallback = (res) {
      completer.complete(res);
    };
    wx.shareToWeChat(model);

    return completer.future;
  }

  // 微信支付
  Future<wx.WeChatPaymentResponse> payWithWeChat(
      {@required String? appId,
      @required String? partnerId,
      @required String? prepayId,
      @required String? packageValue,
      @required String? nonceStr,
      @required int? timeStamp,
      @required String? sign,
      String? signType,
      String? extData}) {
    Completer<wx.WeChatPaymentResponse> completer =
        new Completer<wx.WeChatPaymentResponse>();
    _payWithWeChatCallback = (res) {
      completer.complete(res);
    };
    wx.payWithWeChat(
        appId: appId!,
        partnerId: partnerId!,
        prepayId: prepayId!,
        packageValue: packageValue!,
        nonceStr: nonceStr!,
        timeStamp: timeStamp!,
        sign: sign!,
        signType: signType,
        extData: extData);

    return completer.future;
  }

  // 消息订阅
  Future<wx.WeChatSubscribeMsgResponse> subscribeWeChatMsg({
    @required String? appId,
    @required int? scene,
    @required String? templateId,
    String? reserved,
  }) {
    Completer<wx.WeChatSubscribeMsgResponse> completer =
        new Completer<wx.WeChatSubscribeMsgResponse>();
    _subscribeWeChatMsgCallback = (res) {
      completer.complete(res);
    };
    wx.subscribeWeChatMsg(
        appId: appId!,
        scene: scene!,
        templateId: templateId!,
        reserved: reserved);
    return completer.future;
  }

  //是否安装微信
  Future<bool> isInstallWechat() {
    return wx.isWeChatInstalled;
  }

  //打开微信
  Future<bool> openWeChatApp() {
    return wx.openWeChatApp();
  }
}
